1832B - Maximum Sum - CodeForces Solution


brute force greedy sortings

Please click on ads to support us..

C++ Code:

// بسم الله الرحمن الرحيم
// 907
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define             endl "\n"
#define             ll long long
#define             pii pair<int,int>
#define             pll pair<ll,ll>
#define             f(a) for(int i=0;i<a;i++)
#define             rf(a) for(int i=a-1;i>=0;i--)
#define             w  int t;cin>>t;while(t--)
#define             all(v) v.begin(),v.end()
#define             yes cout<<"YES"<<endl;
#define             no cout<<"NO"<<endl;
#define             prev2pn(x) pow(2,floor(log2(x)))
#define             RR iostream::sync_with_stdio(false);cin.tie(nullptr); \
                    cout.tie(nullptr);
#define             arrRange(a , l , r) int _##a[(r-l)+1]; int* a= _##a-l;
#define             ordered_set tree<pll, null_type,less<pll>, \
                    rb_tree_tag,tree_order_statistics_node_update>
#define             OO 0x3f3f3f3f3f3f3f3f
#define             pr(v) for(int i=0;i<v.size();i++)cout<<v[i]<<" ";
#define             ld long double
#define             fr first
#define             se second
#define             sz(s) (int)s.size()
#define             posmod(number,modular) ((number)%(modular)+(modular))%(modular)
#define             ones(x) __builtin_popcount(x)
const ll MOD=1e9+7;
const int N= 2e5+5;
const int K= 2e5+5;
const double EPS = 1e-6;
const double PI = 3.14159265;

using namespace std;
ll n,m,k;
ll lcm(ll a,ll b){
    return (a*b)/__gcd(a,b);
}

ll powr(ll b,ll p) {
    ll sum = 1;
    for (int i = 1; i <= p; ++i) {
        sum *= b;
    }
    return sum;
}
const int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
vector<int>adj[N];
bool vis[105][105];

ll fast(ll xx, ll nn) {
    ll result = 1;
    while (nn > 0) {
        if ((nn & 1) == 1) // y is odd
        {
            result = (result )%MOD * (xx )%MOD;
        }
        xx = (xx )%MOD * (xx )%MOD;
        nn = nn >> 1; // y=y/2;
    }
    return (result );
}

bool valid(int a,int b){
    return a<0||b<0||a>=n||b>=m;
}

void solve() {
    cin>>n>>m;
    deque<ll> arr(n),b(n),c(n),d(n);
    ll sum1=0,sum2=0,sum3=0,sum4=0;
    for (int i = 0; i < n; ++i) {
        cin>>arr[i];
    }
    sort(all(arr));
    ll pre[n+1],suff[n+1];
    memset(pre,0,sizeof pre);
    for (int i = 0; i <n ; ++i) {
        pre[i]=(i?pre[i-1]:0)+arr[i];
    }
    ll ans=0;
    for (int i = 0; i <=m ; ++i) {
        ll l=i*2,r=n-(m-i)-1;
        ans=max(ans,pre[r]-(l?pre[l-1]:0));
    }
    cout<<ans<<endl;
}
int main() {
    RR
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int q = 1,d=1;
    cin>>q;
    while (q--) {
        solve();
    }
    cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II